package com.cz.nongyetongv5.adapter;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.YangPinXinXiActivity;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.bean.JianChaDanWei;
import com.cz.nongyetongv5.bean.JianChaRenYuan;
import com.cz.nongyetongv5.bean.YangPin;

public class ListViewCheckManagerAdapter extends BaseAdapter {

	private Context context = null;

	private List datas = null;

	/**
	 * 存储是否选择的position 和选择情况
	 */
	private Map<Integer, Boolean> checkMap = new HashMap<Integer, Boolean>();

	private OnCheckedListener onCheckedListener;

	public ListViewCheckManagerAdapter(Context context, List datas) {
		this.datas = datas;
		this.context = context;
		configCheckMap(false);
	}

	public ListViewCheckManagerAdapter(Context context, List datas,
									   OnCheckedListener onCheckedListener) {

		this(context, datas);

		this.onCheckedListener = onCheckedListener;

	}

	/**
	 * 进行数据的初始化
	 */
	public void configCheckMap(boolean bool) {

		for (int i = 0; i < datas.size(); i++) {
			checkMap.put(i, bool);
		}

	}

	@Override
	public int getCount() {
		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewGroup layout = null;

		if (convertView == null) {
			layout = (ViewGroup) LayoutInflater.from(context).inflate(
					R.layout.jianchadanwei_item_layout, null);
		} else {
			layout = (ViewGroup) convertView;
		}

		/**
		 * 每一行的文本数据
		 */
		String lineText = "";

		/**
		 * 默认是可以删除
		 */
		boolean canDelete = true;

		Object data = datas.get(position);

		if (data instanceof JianChaRenYuan) {

			lineText = ((JianChaRenYuan) data).getName();

			canDelete = ((JianChaRenYuan) data).isAllowRemove();

		}

		if (data instanceof JianChaDanWei) {
			lineText = ((JianChaDanWei) data).getName();
			canDelete = ((JianChaDanWei) data).isAllowRemove();
		}

		if (data instanceof YangPin) {

			YangPin yangPin = (YangPin) data;

			lineText = yangPin.getBianHaoString() + "\u0020\u0020" + "["
					+ yangPin.getName() + "]";

			canDelete = true;

		}

		// 设置每一行的文本
		TextView tvTitle = (TextView) layout
				.findViewById(R.id.tvJianChaDanWeiItem);
		tvTitle.setText(lineText);

		// 检查单位的选择
		CheckBox cbCheck = (CheckBox) layout
				.findViewById(R.id.cbJianChaDanWeiItem);

		cbCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
										 boolean isChecked) {

				checkMap.put(position, isChecked);

				if (onCheckedListener != null) {

					int selectCount = 0;

					for (Entry<Integer, Boolean> entrys : checkMap.entrySet()) {

						if (entrys.getValue()) {
							selectCount++;
						}
					}

					onCheckedListener.onChecked(position, selectCount);
				}

			}
		});

		if (!canDelete) {
			// 删除单选框,因为是不可删除的
			cbCheck.setVisibility(View.GONE);
			cbCheck.setChecked(false);

		} else {

			cbCheck.setVisibility(View.VISIBLE);
			/**
			 * 防止删除后报空错误
			 */
			if (checkMap.get(position) == null) {
				checkMap.put(position, false);
			}

			cbCheck.setChecked(checkMap.get(position));

			ViewHolder holder = new ViewHolder();

			holder.cbCheck = cbCheck;

			holder.tvTitle = tvTitle;

			holder.data = data;

			/**
			 * 将数据保存到tag
			 */
			layout.setTag(holder);
		}

		return layout;
	}

	public void add(Object val) {

		this.datas.add(  val);

		configCheckMap(false);

	}

	public void add(Object val, int index) {

		this.datas.add(index, val);

		configCheckMap(false);

	}

	public void remove(int position) {
		this.datas.remove(position);
	}

	public Map<Integer, Boolean> getCheckMap() {
		return this.checkMap;
	}

	public static class ViewHolder {

		public TextView tvTitle = null;

		public CheckBox cbCheck = null;
		public Object data = null;

	}

	public Integer getSingleCheckPosition() {

		int selectCount = 0;

		Integer position = null;

		for (Entry<Integer, Boolean> entrys : checkMap.entrySet()) {

			if (entrys.getValue()) {
				selectCount++;
				position = entrys.getKey();
			}
		}

		if (selectCount == 1 && position != null) {

			return position;
		} else {

			return null;
		}

	}

	public List getDatas() {
		return datas;
	}

	public int getInsertIndex() {

		int index = 0;

		if (this.datas != null && this.datas.size() > 0) {

			for (int i = 0; i < this.datas.size(); i++) {

				Object obj = this.datas.get(i);

				try {
					Field field = obj.getClass().getDeclaredField(
							"isAllowRemove");

					field.setAccessible(true);

					boolean isAllowRemove = field.getBoolean(obj);

					if (!isAllowRemove) {
						index = i + 1;
						continue;
					} else {
						break;
					}

				} catch (NoSuchFieldException e) {
				} catch (IllegalAccessException e) {
				} catch (IllegalArgumentException e) {
				}

			}

		}

		return index;

	}

	/**
	 * 当选择的时候
	 */
	public static interface OnCheckedListener {
		public void onChecked(int position, int selectCount);
	}

}
